package com.magicborrow.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.magicborrow.BaseLog;
import com.magicborrow.Constants;
import com.magicborrow.R;
import com.magicborrow.activity.AppendCommentActivity;
import com.magicborrow.activity.AppraiseActivity;
import com.magicborrow.beans.ApplyEntity;
import com.magicborrow.beans.BaseBean;
import com.magicborrow.enums.PriceModeEnum;
import com.magicborrow.utils.ImageLoadUtil;
import com.magicborrow.utils.UserTools;
import com.magicborrow.utils.VolleyTool;
import com.magicborrow.views.LoadingDialog;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by ghost on 16/3/28.
 */
public class CommentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_FOOT = 0;
    private static final int TYPE_NORMAL = 1;
    private ArrayList<ApplyEntity.ContentData> mData;
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private int targetType;

    public CommentAdapter(Context pContext, int targetType) {
        this.mContext = pContext;
        mLayoutInflater = LayoutInflater.from(mContext);
        this.targetType = targetType;
    }

    public void setData(ArrayList pData) {
        this.mData = pData;
    }

    public ArrayList<ApplyEntity.ContentData> getData() {
        return this.mData;
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOT;
        } else {
            return TYPE_NORMAL;
        }
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_FOOT) {
            View view = mLayoutInflater.inflate(R.layout.load_more_footer, null);
            view.setTag("foot");
            return new FootViewHolder(view);
        } else if (viewType == TYPE_NORMAL) {
            View view = mLayoutInflater.inflate(R.layout.comment_item, null);
            return new CommentAdapter.CommentItemViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof CommentItemViewHolder) {
            CommentItemViewHolder hold = (CommentItemViewHolder) holder;
            final ApplyEntity.ContentData bean = mData.get(position);
            BaseLog.e("ssss", Constants.ADDRESS + bean.getUser().getAvatar80());
            ImageLoadUtil.loadImage(hold.imv_head, Constants.ADDRESS + bean.getTarget().getAvatar80().split("\\?")[0], R.mipmap.head_default);
            String url = "";
            if (bean.getWare().getImages() != null && bean.getWare().getImages().size() > 0) {
                url = Constants.ADDRESS + bean.getWare().getImages().get(0).getPreview();
            }
            ImageLoadUtil.loadImage(hold.good_pic, url, R.mipmap.img_default);
            hold.good_name.setText(bean.getWare().getName());
            hold.tv_name.setText(bean.getTarget().getNickname());
            hold.tv_address.setText("地址: " + bean.getWare().getAddress());
            hold.borrowTime.setText("借用时间: " + bean.getCreated());
            hold.borrowShichang.setText("借用时长: " + bean.getNumber() + PriceModeEnum.getModeStr(bean.getWarePriceMode()));
            float lendScaleAVG = bean.getUser().getLendScaleAVG();//借出者的信用评分
            hold.commentCountMsg.setText(lendScaleAVG + "分");
            hold.rb_pingfen.setRating(lendScaleAVG);
            hold.shouldPay.setText("应付金额: ￥" + (bean.getNumber() * bean.getWarePrice() + bean.getWareDeposit()));
            hold.tvDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    HashMap<String, String> params = new HashMap<String, String>();
                    if (targetType == 1) {
                        params.put("id", bean.getUserCommentId() + "");

                    } else {
                        params.put("id", bean.getTargetCommentId() + "");
                    }
                    params.put("acc_token", UserTools.getUser(mContext).getAcc_token());
                    final LoadingDialog dialog = new LoadingDialog(mContext);
                    dialog.show();
                    VolleyTool.get(Constants.URL_GET_COMMENT_DELETE, params, new VolleyTool.HTTPListener() {
                        @Override
                        public <T> void onResponse(T t, int requestCode) {
                            BaseBean b = (BaseBean) t;
                            dialog.dismiss();
                            if (b.getCode() >= 0) {
                                notifyItemRemoved(position);
                                mData.remove(position);
                            } else {
                                Toast.makeText(mContext, b.getMessage(), Toast.LENGTH_SHORT).show();
                            }


                        }

                        @Override
                        public void onErrorResponse(VolleyError error, int requestCode) {
                            dialog.dismiss();
                        }
                    }, 0, BaseBean.class);
                }
            });

            if (bean.getCommented() == 0 || (targetType == 1 && bean.getCommented() != 1) || (targetType == 2 && bean.getCommented() != 2)) {
                hold.tvDelete.setVisibility(View.GONE);
                hold.comment.setText("评论");
                hold.comment.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent();
                        intent.putExtra("targetType", targetType);
                        intent.putExtra("targetId", bean.getWare().getId());
                        intent.putExtra("applyId", bean.getId());
                        intent.putExtra("toUserId", targetType == 1 ? bean.getTarget().getId() : bean.getUser().getId());
                        intent.setClass(mContext, AppraiseActivity.class);
                        mContext.startActivity(intent);
                    }
                });
            } else if (targetType == 1) {
                hold.tvDelete.setVisibility(View.VISIBLE);
                hold.comment.setText("追加评论");
                hold.comment.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent();
                        intent.putExtra("targetType", targetType);
                        intent.putExtra("targetId", bean.getWare().getId());
                        intent.putExtra("applyId", bean.getId());
                        intent.putExtra("toUserId", targetType == 1 ? bean.getTarget().getId() : bean.getUser().getId());
                        intent.setClass(mContext, AppendCommentActivity.class);
                        mContext.startActivity(intent);
                    }
                });

            } else {
                hold.tvDelete.setVisibility(View.VISIBLE);
                hold.comment.setText("已评论");
            }


        }
    }

    @Override
    public int getItemCount() {
        return mData.size() + 1;
    }


    public class CommentItemViewHolder extends RecyclerView.ViewHolder {
        private ImageView imv_head;
        private TextView tv_name;
        private TextView statusMsg;
        private RatingBar rb_pingfen;
        private TextView commentCountMsg;
        private ImageView good_pic;
        private TextView good_name;
        private TextView borrowTime;
        private TextView borrowShichang;
        private TextView tv_address;
        private TextView shouldPay;
        private TextView comment;
        private TextView tvDelete;

        public CommentItemViewHolder(View view) {
            super(view);
            imv_head = (ImageView) view.findViewById(R.id.imv_head);
            tv_name = (TextView) view.findViewById(R.id.tv_name);
            statusMsg = (TextView) view.findViewById(R.id.statusMsg);
            rb_pingfen = (RatingBar) view.findViewById(R.id.rb_pingfen);
            commentCountMsg = (TextView) view.findViewById(R.id.commentCountMsg);
            good_pic = (ImageView) view.findViewById(R.id.good_pic);
            good_name = (TextView) view.findViewById(R.id.good_name);
            borrowTime = (TextView) view.findViewById(R.id.borrowTime);
            borrowShichang = (TextView) view.findViewById(R.id.borrowShichang);
            tv_address = (TextView) view.findViewById(R.id.tv_address);
            shouldPay = (TextView) view.findViewById(R.id.shouldPay);
            comment = (TextView) view.findViewById(R.id.comment);
            tvDelete = (TextView) view.findViewById(R.id.tv_delete);
        }
    }

    public class FootViewHolder extends RecyclerView.ViewHolder {

        public FootViewHolder(View itemView) {
            super(itemView);
        }

    }

}
